/*
 *******************************************************************************
 * All rights Reserved, Copyright (C) www.gm-sz.com 2013
 * FileName: leftbar.js
 * Modify record:
 * NO. |     Date       |    Version      |    Name         |      Content
 * 1   | 2013-11-11     |       1.0       |  GMSZ)GuoRuofei | original version
 *******************************************************************************
 */

/**
 * 左边栏
 */
var leftbar = leftbar || {};
leftbar.sidebarFold=new Array();//1是收缩，2是打开

leftbar.top = 0;
leftbar.step = 90;

$(function() {
	$(window).resize(leftbar.showCurrent);
	leftbar.initFold();
});

/**
 * Description: 将当前选中的菜单项移动至可视窗口内
 */
leftbar.showCurrent = function() {
	leftbar.top = $('.lay-left').height() - $('.lay-left-bar .com-active').position().top - leftbar.step;
	leftbar.setScroll();
};

/**
 * Description: 菜单窗口上移
 */
leftbar.showUp = function() {
	leftbar.top += leftbar.step;
	leftbar.setScroll();
};

/**
 * Description: 菜单窗口下移
 */
leftbar.showDown = function() {
	leftbar.top -= leftbar.step;
	leftbar.setScroll();
};

/**
 * Description: 移动窗口，根据当前位置切换控制按钮的可用状态
 */
leftbar.setScroll = function() {
	var max = $('.lay-left-bar').height() - $('.lay-left').height();
	$('.lay-left-up').prop('disabled', false);
	$('.lay-left-down').prop('disabled', false);
	if (leftbar.top >= 0) {
		leftbar.top = 0;
		$('.lay-left-up').prop('disabled', true);
	}
	if (leftbar.top + max <= 0) {
		leftbar.top = -max;
		$('.lay-left-down').prop('disabled', true);
	}
	$('.lay-left-bar').css({
		top : leftbar.top
	});
};


leftbar.initFold=function(){
	var menufold=$("#menufold").val();//上一次菜单是否打开
	if(menufold){//已选中过
		var menufolds=menufold.split("|");
		for(var i=0;i<menufolds.length;i++){
			var temp=menufolds[i].split(".");
			if(temp[0]!=''){
				var topMenuDiv=new MenuFlod(temp[0],temp[1]);
				leftbar.sidebarFold.push(topMenuDiv);
			}
			
		}
	}else{//查询一级菜单下的二级菜单div
		var   leftMenuFold = $("div[name='left_menu_fold']") ;
		for(var i=0;i<leftMenuFold.length;i++){
			var menuId=$(leftMenuFold[i]).attr("menuId");
			var topMenuDiv=new MenuFlod(menuId,1);
			if(menuId==10){
				topMenuDiv=new MenuFlod(menuId,2);
			}
			
			leftbar.sidebarFold.push(topMenuDiv);
		}
	}
	for(var i=0;i<leftbar.sidebarFold.length;i++){
		leftbar.setFold(leftbar.sidebarFold[i].menuId);
	}
	
}

/**一级菜单是否打开 2是打开，1是关闭*/
function MenuFlod(menuId,status){
	this.menuId=menuId;
	this.status=status;
}

/**需要把菜单是打开还是收缩的传过去*/
leftbar.openPage=function(menuId,url){
	leftbar.saveSet(menuId,2,url);
}
/**切换菜单收缩和打开*/
leftbar.toggleFoldStatus=function(menuId){
	
	var index=leftbar.getSidebarFold(menuId);
	var currstatus=leftbar.sidebarFold[index].status;
	if(currstatus==2){
		leftbar.sidebarFold[index].status=1;
	}else{
		leftbar.sidebarFold[index].status=2;
	}
	leftbar.setFold(menuId);
	
	var menufold="";
	for(var i=0;i<leftbar.sidebarFold.length;i++){
		var status=leftbar.sidebarFold[i].status;
		if(leftbar.sidebarFold[i].menuId!=menuId){
			leftbar.sidebarFold[i].status=1;
			status=1;
			$("#left_menu_fold_"+leftbar.sidebarFold[i].menuId).hide();
			$("#left_menu_fold_img_"+leftbar.sidebarFold[i].menuId).attr("src",commonutil.pagePath+"/images/icon/icon_on_22.png");
		}
		menufold+=leftbar.sidebarFold[i].menuId+"."+status+"|";
	}
	leftbar.saveSet(menuId,1,null,menufold);
}

/**保存页面设置*/
leftbar.saveSet=function(menuId,lvl,openUrl,menufold){
	var url = commonutil.actionPath + '/frame/menu/chkMenuId';
	commonutil.doAjax(url, {'menuId':menuId ,'Lvl':lvl,'menufold':menufold}, function(data) {
		//alert(openUrl)
		if(openUrl){
			window.location =openUrl;
		}
	}, 'json');
}


/**设置菜单是打开还是收缩*/
leftbar.setFold=function(menuId){
	var index=leftbar.getSidebarFold(menuId);
	var fold= leftbar.sidebarFold[index].status;
	if(fold==2){
		$("#left_menu_fold_"+menuId).show();
		$("#left_menu_fold_img_"+menuId).attr("src",commonutil.pagePath+"/images/icon/icon_on_21.png");
	}else{
		$("#left_menu_fold_"+menuId).hide();
		$("#left_menu_fold_img_"+menuId).attr("src",commonutil.pagePath+"/images/icon/icon_on_22.png");
	}
}

leftbar.getSidebarFold=function(menuId){
	for(var i=0;i<leftbar.sidebarFold.length;i++){
		if(leftbar.sidebarFold[i].menuId==menuId){
			return i;
		}
	}
}

/**跳转到病人信息详细页*/
leftbar.openPatientDetail=function(obj){
	var patientId=$(obj).attr("patientid");
	if(patientId){
		var url = commonutil.actionPath + '/addPersonalHistoryInit?patientId='+patientId;
		leftbar.toggleFoldStatus(2);
		leftbar.saveSet(10,2,url);
	}
};

